//
//  AFXNetworkTools.m
//  CZ爱鲜蜂
//
//  Created by 程雷 on 2017/2/18.
//  Copyright © 2017年 陈帅. All rights reserved.
//

#import "AFXNetworkTools.h"
#import <AFNetworking.h>

@implementation AFXNetworkTools{

    AFHTTPSessionManager *_manager;
}

static id _instance;
+(instancetype)sharedManager{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}
-(instancetype)init{

    if (self = [super init]) {
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        configuration.timeoutIntervalForRequest = 15;
        _manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];
    }
    return self;
}


-(void)requestNetworkingWithMethod:(requestMethod)method url:(NSString *)urlString parameters:(id)parameters completeBlock:(void (^)(id, NSError *))complete{

    if (method == Method_get) {
        [_manager GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            complete(responseObject,nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            complete(nil,error);
        }];
        
    }else{
    
        [_manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            complete(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            complete(nil,error);
        }];
    }
}


@end
